package com.carsystems.controller;

import com.carsystems.mapper.UserCarDetailsMapper;
import com.carsystems.pojo.OperatingCompany;
import com.carsystems.pojo.UserCarDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserCarDetailsController {

    @Autowired
    private UserCarDetailsMapper userCarDetailsMapper;

//一对多
//    @GetMapping("/querList")
//    public List<UserCarDetails> querList() {
//        List<UserCarDetails> userCarDetails = userCarDetailsMapper.querList();
//        return userCarDetails;
//    }


    //查看全部
    @GetMapping("/querUserList")
    public List<UserCarDetails> querUserList() {
        List<UserCarDetails> userCarDetails = userCarDetailsMapper.querUserList();
        return userCarDetails;
    }

    //通过user_id查询员工信息
    @GetMapping("/findUserByuserid")
    public UserCarDetails getByid(Integer user_id) {
        return userCarDetailsMapper.selectByPrimaryKey(user_id);
    }

    //增加一个员工
    @ResponseBody
    @PostMapping("/addUser")
    public String addUser(@RequestBody UserCarDetails user) {
       userCarDetailsMapper.addUser(user);
        if (user != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改员工信息
    @ResponseBody
    @PostMapping("/updateUser")
    public String updateUser(@RequestBody UserCarDetails user){
        int row = userCarDetailsMapper.updateUser(user);
       userCarDetailsMapper.selectByPrimaryKey(user.getUser_id());
       if (row==1){
           return "success";
       }else {
           return "fail";
       }
    }

    //删除员工
    @ResponseBody
    @GetMapping("/daleteUserByUserid")
    public String daleteUserByUserid(UserCarDetails userCarDetails){
       int row = userCarDetailsMapper.daleteUser(userCarDetails.getUser_id());
       if (row == 1){
           return "success";
       }else {
           return "fail";
       }
    }
}
